import 'package:intl/intl.dart';
import 'package:share_fit/app/helper/ext/tr_ext.dart';
import 'package:share_fit/app/models/base_model.dart';
import 'package:share_fit/generated/locales.g.dart';

/// 自定义小组件model类
class EditWidgetItem extends BaseModel {
  /// 小组件信息，用于GetBuilder更新
  late Type type;
  late String title;
  late double? width;
  late double? height;
  late String? action;
  late String? image;
  String? route;

  EditWidgetItem({
    required this.type,
    required this.title,
    this.width,
    this.height,
    this.action = 'add',
    this.image,
    this.route,
  });

  EditWidgetItem.init();

  @override
  toMap() {
    return {'type': type.code, 'title': title, 'width': width, 'height': height, 'action': action, 'image': image, 'route': route};
  }

  @override
  fromList(dynamic maps) {
    List<EditWidgetItem> list = [];
    for (var map in maps) {
      EditWidgetItem item = EditWidgetItem.init();
      if (item.from(map) && !list.contains(item)) {
        list.add(item);
      }
    }
    return list;
  }

  @override
  bool from(Map map) {
    type = Type.getByCode(map['type']);
    title = type.getTitle;
    width = map['width'];
    height = map['height'];
    action = map['action'];
    image = map['image'];
    route = map['route'];
    return true;
  }

  @override
  List<Object?> get props => [title, type];
}

enum Type {
  heartRate(code: 'home_widget_hr'),
  bloodOxygen(code: 'home_widget_bo'),
  bloodPressure(code: 'home_widget_bp'),
  sleep(code: 'home_widget_sleep'),
  stress(code: 'home_widget_stress'),
  cycleCalendar(code: 'home_widget_cc'),
  exerciseRecords(code: 'home_widget_er'),
  bodyTemperature(code: 'home_widget_bt'),
  bloodGlucose(code: 'home_widget_bg'),
  met(code: 'home_widget_met'),
  fatigue(code: 'home_widget_fatigue');

  const Type({required this.code});

  final String code;

  get getTitle {
    switch(code) {
      case 'home_widget_hr':
        return LocaleKeys.widgets_heart_rate.ts;
      case 'home_widget_bo':
        return LocaleKeys.widgets_blood_oxygen.ts;
      case 'home_widget_bp':
        return LocaleKeys.widgets_blood_pressure.ts;
      case 'home_widget_sleep':
        return LocaleKeys.widgets_sleep.ts;
      case 'home_widget_stress':
        return LocaleKeys.widgets_stress.ts;
      case 'home_widget_cc':
        return LocaleKeys.widgets_cycle_calendar.ts;
      case 'home_widget_er':
        return LocaleKeys.widgets_exercise.ts;
      case 'home_widget_bt':
        return LocaleKeys.widgets_body_temperature.ts;
      case 'home_widget_bg':
        return LocaleKeys.widgets_blood_glucose.ts;
      case 'home_widget_met':
        return LocaleKeys.widgets_met.ts;
      case 'home_widget_fatigue':
        return LocaleKeys.widgets_fatigue.ts;
      default:
        return '';
    }
  }

  get indicatorMap {
    switch (code) {
      case 'home_widget_sleep':
        return {'start': LocaleKeys.sleep_quality_bad_text.ts, 'end': LocaleKeys.sleep_quality_good_text.ts};
      case 'home_widget_cc':
        DateTime now = DateTime.now();
        DateTime start = DateTime(now.year, now.month, 1);
        DateTime end = DateTime(now.year, now.month + 1, 1).add(const Duration(days: -1));
        return {'start': DateFormat('MM/dd').format(start), 'end': DateFormat('MM/dd').format(end)};
      default:
        return {'start': '00:00', 'end': '24:00'};
    }
  }

  static Type getByCode(String code) {
    return Type.values.firstWhere((element) => element.code == code);
  }
}
